﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Chenxin.Lib.Utility;
public partial class WorkReport_WorkReportDetailInfo : PageBase
{
    string strRecordId = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(Request.Params["RecordId"])) Response.Redirect("~/errorpage.aspx?msg=非法工作报告ID", true);
            if(!IsPostBack)
            {
                strRecordId = Request.Params["RecordId"].ToString().Trim();
                hidRecordId.Value = strRecordId.ToString();
                Chenxin.Lib.Hr.Entity.WorkReport_OutBox workreport_outbox = GetModel(new Guid(strRecordId));
                BindWorkTable(workreport_outbox);
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("~/errorpage.aspx?msg=" + ex.Message + "", true);
        }
    }

    private Chenxin.Lib.Hr.Entity.WorkReport_OutBox GetModel(Guid strRecordId)
    {
        Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);

        Chenxin.Lib.Hr.Entity.WorkReport_OutBox workreport_outbox;
        //查找Cache的用户权限键
        string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_WORKREPORT, strRecordId.ToString());
        //从Cache中取得工作报告
        workreport_outbox = cs.GetObject<Chenxin.Lib.Hr.Entity.WorkReport_OutBox>(strCacheKey);
        //未命中
        if (!cs.Hit)
        {
            //从数据库中搜索
            workreport_outbox = Chenxin.Lib.Hr.BLL.WorkReport_OutBox.GetModel(strRecordId);
            //加入到Cache中
            cs.Set(strCacheKey, workreport_outbox);
        }
        return workreport_outbox;
    }

    private void BindWorkTable(Chenxin.Lib.Hr.Entity.WorkReport_OutBox workreport_outbox)
    {
        StringBuilder sb = new StringBuilder();
        string filePath = System.Configuration.ConfigurationManager.AppSettings["FielPath"];
        DataSet ds = Chenxin.Lib.Hr.BLL.Attachment.GetList(string.Format(" RelationId = '{0}' and [Type]=10 ", new Guid(strRecordId)));

        sb.Append("<table width=\"100%\" cellspacing=\"1\" class=\"tabsorternew tab_workreport\" id=\"worktable\">");
        sb.Append("<thead>");
        sb.Append("<tr>");
        sb.AppendFormat("<th colspan=\"2\"><span>【标题】 {0}</span></th>", workreport_outbox.Subject.ToString());
        sb.Append("</tr>");
        sb.Append("</thead>");
        sb.Append("<tbody>");

        sb.Append("<tr>");
        sb.Append("<td style=\"width:180px; vertical-align:1px;\"> ");
        sb.AppendFormat("<div style='padding:22px 0 0 7px;'><span style='line-height:20px;'><span>发送者工号：{0}</span><br>", workreport_outbox.SenderCode.ToString());
        sb.AppendFormat("<span>发送者姓名：{0}</span><br>", workreport_outbox.SenderName.ToString());
        sb.AppendFormat("<span>发送时间：{0}</span><br>", workreport_outbox.SendTime.ToString());
        sb.AppendFormat("<span>接收对象：{0}</span><br>", workreport_outbox.ReceiverName.ToString());
        if (ds.Tables[0].Rows.Count != 0 && System.IO.File.Exists(Server.MapPath(Constants.DIRECTORY + ds.Tables[0].Rows[0]["Path"].ToString())))
        {
            string pathname = ds.Tables[0].Rows[0]["FileName"].ToString();
            if (!string.IsNullOrEmpty(pathname) && pathname.Length > 20)
            {
                pathname = pathname.Substring(0, 20) + "...";
            }
            sb.AppendFormat("<span>附件：<a id=\"abtnreply\" href=\"/service/Download.ashx?file=/{0}\" title=\"{1}\">{2}</a></span><br>", ds.Tables[0].Rows[0]["Path"].ToString(), ds.Tables[0].Rows[0]["FileName"].ToString(), pathname);
        }
        else
        {
            sb.Append("<span>附件：无附件</span><br>");
        }

        sb.Append("</span></div><div>");
        if (workreport_outbox.SenderId.Equals(CurrentUserInfo.UserId))
        {
            sb.Append("<div class=\"btn\"><a href=\"javascript:void(0)\" title=\"修改\" id=\"btn_edit\" name=\"" + strRecordId + "\"><span>修改</span></a></div>");
            sb.Append("<div class=\"btn\"><a href=\"javascript:void(0)\" title=\"删除\" id=\"btn_del\" name=\"" + strRecordId + "\"><span>删除</span></a></div>");
        }
        sb.Append("</div>");
        sb.Append("<div class=\"btn\"><a href=\"javascript:void(0)\" title=\"转发\" id=\"btn_transmit\" name=\"" + strRecordId + "\"><span>转发</span></a></div>");
        sb.Append("<div class=\"btn\"><a href=\"javascript:void(0)\" title=\"回复\" id=\"btn_reply\"><span>回复</span></a></div>");
        sb.Append("<div class=\"btn\"><a href=\"javascript:void(0)\" title=\"复制到剪切板\" id=\"btn_copycon\"><span>复制正文</span></a></div>");

        sb.Append("</td>");
        sb.Append("<td>");
        sb.AppendFormat("<div style='padding:10px;'><span style='line-height:normal;'>{0}</span></div>", Server.UrlDecode(workreport_outbox.Content.ToString()));
        if (!workreport_outbox.RootId.Equals(Guid.Empty))
        {
            Chenxin.Lib.Hr.Entity.WorkReport_OutBox model = Chenxin.Lib.Hr.BLL.WorkReport_OutBox.GetModel(workreport_outbox.RootId);
            sb.AppendFormat("<div style='padding:10px; position:relative; bottom:-40px;'><a href=\"WorkReportDetailInfo.aspx?RecordId={0}\" title=\"原工作报告：{1}\" id=\"btn_fatherurl\"><span>原工作报告：{1}</span></a></div>", workreport_outbox.RootId.ToString(), model.Subject.ToString());
        }
        sb.Append("</td></tr></tbody></table>");

        sb.Append("<div style=\"display:none; border:1px solid #C5DDF6; padding: 25px 100px 25px; border-top:0px\" id=\"divreply\">");
            sb.Append("<table>");
                sb.Append("<tr>");
                    sb.Append("<td>");
                        sb.Append("<span>回复主题：</span>");
                    sb.Append("</td>");
                    sb.Append("<td>");
                        sb.AppendFormat("<input type=\"text\" class=\"inputbox_n fl\" style=\"width:99%\" name=\"Subject\" id=\"txtSubject\" value='回复：{0}'/>", workreport_outbox.Subject.ToString());
                    sb.Append("</td>");
                sb.Append("</tr>");
                sb.Append("<tr>");
                    sb.Append("<td>");
                        sb.Append("<span>回复正文：</span>");
                    sb.Append("</td>");
                    sb.Append("<td>");
                        sb.Append("<textarea id=\"txtcontent\" name=\"Content\" cols=\"80\" rows=\"20\" class=\"textarea_n\" style=\"width:700px; height:170px; padding:5px;\"></textarea>");
                    sb.Append("</td>");
                sb.Append("</tr>");
                sb.Append("<tr style=\"height:25px;\">");
                    sb.Append("<td>");
                        sb.Append("<span>上传附件：</span>");
                    sb.Append("</td>");
                    sb.Append("<td><div>");
                    sb.Append("<div><input type=\"file\" name=\"thumbnialUploadify\" id=\"thumbnialUploadify\"/></div>");
                    sb.Append("</div></td>");
                sb.Append("</tr>");
                sb.Append("<tr style=\"height:25px;\">");
                    sb.Append("<td>");
                        sb.Append("<span> </span>");
                    sb.Append("</td>");
                    sb.Append("<td>");
                    sb.Append("<div style=\"position:relative; right:-650px;\"><div class=\"btn\"><a href=\"javascript:void(0)\" title=\"提交\" id=\"btn_addreply\"><span>提 交</span></a></div></div>");
                    sb.Append("</td>");
                sb.Append("</tr>");
            sb.Append("</table>");
        sb.Append("</div>");

        this.ltlWorkTable.Text = sb.ToString();

        if (!workreport_outbox.RootId.Equals(Guid.Empty))
        {
            hidRootId.Value = workreport_outbox.RootId.ToString();
        }
        else
        {
            hidRootId.Value = strRecordId.ToString();
        }
    }

}
